package com.jk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.jk.mapper.WJSMapper;
import com.jk.model.*;
import com.jk.service.WJSService;

import com.jk.utils.Md5Util;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Component
public class WJSServiceImpl implements WJSService {

    @Autowired
    private WJSMapper wjsMapper;

    @Override
    public void register(UserBean userBean) {
        wjsMapper.register(userBean);
    }

    @Override
    public UserBean queryUser(UserBean userBean) {
        return wjsMapper.queryUser(userBean);
    }

    //买家查询
    @Override
    public Map findUserList(Integer page, Integer rows, UserBean userBean,Integer id) {

        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> params = new HashMap<>();
        //
        params.put("userBean",userBean);
        params.put("id",id);
        //
        Integer count = wjsMapper.findUserGe(params);
        result.put("total",count);
        //list
        params.put("startIndex",(page-1)*rows);
        params.put("endIndex",rows);
        //
        List<UserBean> userList = wjsMapper.findUserList(params);
        result.put("rows",userList);
        return result;
    }

    @Override
    public Map yimai(Integer page, Integer rows, UserBean userBean,Integer id) {
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> params = new HashMap<>();
        //
        params.put("userBean",userBean);
        params.put("id",id);
        //
        Integer count = wjsMapper.findcount(params);
        result.put("total",count);
        //list
        params.put("startIndex",(page-1)*rows);
        params.put("endIndex",rows);
        //
        List<UserBean> userList = wjsMapper.yimai(params);
        result.put("rows",userList);
        return result;
    }

    @Override
    public List<ProductBean> shangpin() {

        return wjsMapper.shangpin();
    }

    //待付款
    @Override
    public Map daifukuan(Integer page, Integer rows, UserBean userBean,Integer id) {
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> params = new HashMap<>();
        //
        params.put("userBean",userBean);
        params.put("id",id);
        //
        Integer count = wjsMapper.find2(params);
        result.put("total",count);
        //list
        params.put("startIndex",(page-1)*rows);
        params.put("endIndex",rows);
        //
        List<UserBean> userList = wjsMapper.daifukuan(params);
        result.put("rows",userList);
        return result;
    }

    //待发货
    @Override
    public Map daifahuo(Integer page, Integer rows, UserBean userBean,Integer id) {
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> params = new HashMap<>();
        //
        params.put("userBean",userBean);
        params.put("id",id);
        //
        Integer count = wjsMapper.find3(params);
        result.put("total",count);
        //list
        params.put("startIndex",(page-1)*rows);
        params.put("endIndex",rows);
        //
        List<UserBean> userList = wjsMapper.daifahuo(params);
        result.put("rows",userList);
        return result;
    }

    @Override
    public Map shangdian(Integer page, Integer rows, UserBean userBean,Integer id) {
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> params = new HashMap<>();
        //
        params.put("userBean",userBean);
        params.put("id",id);
        //
        Integer count = wjsMapper.dianpucount(params);
        result.put("total",count);
        //list
        params.put("startIndex",(page-1)*rows);
        params.put("endIndex",rows);
        //
        List<UserBean> userList = wjsMapper.shangdian(params);
        result.put("rows",userList);
        return result;
    }

    //递归树
    @Override
    public List<MaiTree> findMaiList() {
        Integer id=0;
        List<MaiTree> navs = navNodes(id);
        return navs;
    }

    private List<MaiTree> navNodes(Integer id) {
        List<MaiTree> navs = wjsMapper.findChildren(id);
        for (MaiTree maiTree : navs) {
            List<MaiTree> navNodes = navNodes(maiTree.getId());
            if (navNodes == null || navNodes.size() <= 0){
                maiTree.setSelectable(true);
            }else{
                maiTree.setNodes(navNodes);
            }
        }
        return navs;
    }

    //收货地址
    @Override
    public Map dizi(Integer page, Integer rows, UserAcceptsite userAcceptsite,Integer id) {

        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> params = new HashMap<>();
        //
        params.put("userAcceptsite",userAcceptsite);
        params.put("id",id);
        //
        Integer count = wjsMapper.dizigeshu(params);
        result.put("total",count);
        //list
        params.put("startIndex",(page-1)*rows);
        params.put("endIndex",rows);
        //
        List<UserAcceptsite> userList = wjsMapper.dizi(params);
        result.put("rows",userList);
        return result;
    }

    @Override
    public Map findgeren(Integer page, Integer rows, UserBean userBean, Integer id) {
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> params = new HashMap<>();
        //
        params.put("userBean",userBean);
        params.put("id",id);
        //
        Integer count = wjsMapper.geshu(params);
        result.put("total",count);
        //list
        params.put("startIndex",(page-1)*rows);
        params.put("endIndex",rows);
        //
        List<UserBean> userList = wjsMapper.findgeren(params);
        result.put("rows",userList);
        return result;
    }

    @Override
    public void updatePassword(String password, Integer userId) {
        wjsMapper.updatePassword(password,userId);
    }

    @Override
    public Map codePassword(String userPassword, HttpSession session) {
        HashMap<String, Object> result = new HashMap<>();
        UserBean userBean = (UserBean) session.getAttribute(session.getId());
        String userPassword1 = userBean.getUserPassword();
        if (!userPassword.equals(userPassword1)) {
            result.put("code", 1);
            result.put("msg", "密码输入不正确");
            return result;
        }
        //成功
        result.put("code", 2);
        result.put("msg", userBean.getId());
        return result;
    }
}
